<?php

namespace App\Service\UserCenter;

use App\Helpers\ConfigUtil;
use App\Model\Service\CacheService;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 收费设置列表
 */
class CoinSettingService extends BaseService
{
    public function handle()
    {
        $isNewVersion = $this->curVersion >= '1.1.5';
        //收费类型：0-默认语音，1-视频，2-私聊收费
        $coinType = $this->request->input('coin_type', 0);
        // if ($isNewVersion && $coinType == 2) return [];

        $userId = $this->getUserId();
        $profile = UserProfile::getProfile($userId);
        //设置
        switch ($coinType) {
                //语音
            case 0:
                $text = "接听语音";
                $coinField = 'coin_setting';
                break;
                //视频
            case 1:
                $text = "接听视频";
                $coinField = 'video_coin_setting';
                break;
                //文字
            case 2:
                $text = "接收私信";
                $coinField = 'chat_coin_setting';
                break;
        }
        if ($isNewVersion) {
            $coinField == 'coin_setting' && $coinField = 'voice_coin_setting';
            $settings = CacheService::getCoinSetting($coinField);
        } else {
            $settings = ConfigUtil::getCoinSetting($coinType);
        }

        array_walk($settings, function (&$setting) use ($profile, $coinField, $text) {
            $setting['text'] = $setting['coin'] == 0 ? '接收私信免费' : "{$text}{$setting['coin']}金币/条";
            if ($setting['charm_limit']) $setting['sub_text'] = "魅力值{$setting['charm_limit']}可选";
            else $setting['sub_text'] = "认证后可选";
            $setting['selected'] = $profile && $profile->{$coinField} == $setting['coin'] ? 1 : 0;
        });
        // $ret['list'] = $settings;
        return $settings;
    }
}
